<!-- 明细数据统计 -->
<template>
    <div >
        <a-card :bordered="false" title="明细数据统计">
            <a-row>
                <a-col :sm="8" :xs="24">
                    <Info title="结算笔数（笔）" :value="totalCount"/>
                </a-col>
                <a-col :sm="8" :xs="24">
                    <Info title="成功笔数（笔）" :value="successCount"/>
                </a-col>
                <a-col :sm="8" :xs="24">
                    <Info title="失败笔数（笔）" :value="failCount" :rightBordered="false" />
                </a-col>
            </a-row>
            <a-row>
                <a-col :sm="8" :xs="24">
                    <Info title="结算金额（￥）" :value="totalAmount" :btmBordered="false"/>
                </a-col>
                <a-col :sm="8" :xs="24">
                    <Info title="成功金额（￥）" :value="successAmount" :btmBordered="false"/>
                </a-col>
                <a-col :sm="8" :xs="24">
                    <Info title="失败金额（￥）" :value="failAmount" :rightBordered="false" :btmBordered="false"/>
                </a-col>
            </a-row>
        </a-card>

        <!-- 列表数据 -->
        <a-card :bordered="false" style="margin-top: 20px" title="列表数据">
            <div class='table-page-search-wrapper'>
                <a-form layout='inline'>
                    <a-row :gutter='48'>
                        <a-col :md='8' :sm='24'>
                            <a-form-item label="用户信息">
                                <a-input v-model="query" placeholder="请输入姓名或身份证号" allow-clear></a-input>
                            </a-form-item>
                        </a-col>
                        <a-col :md='8' :sm='24'>
                            <a-form-item label="项目名称">
                                <a-input v-model="projectName" placeholder="请输入项目名称" allow-clear></a-input>
                            </a-form-item>
                        </a-col>
                        <a-col :md='8' :sm='24'>
                            <a-form-item label="所属客户">
                                <a-input v-model="customerName" placeholder="请输入所属客户" allow-clear></a-input>
                            </a-form-item>
                        </a-col>
                        <a-col :md='8' :sm='24'>
                            <a-form-item label="结算单名称">
                                <a-input v-model="settlementName" placeholder="请输入结算单名称" allow-clear></a-input>
                            </a-form-item>
                        </a-col>
                        <a-col :md='8' :sm='24'>
                            <a-form-item label="所属公司">
                                <a-input v-model="companyName" placeholder="请输入所属公司" allow-clear></a-input>
                            </a-form-item>
                        </a-col>
                        <a-col :md='16' :sm='24'>
                            <a-form-item label="时间">
                                <a-range-picker @change="onDateChange" allow-clear/>
                            </a-form-item>
                        </a-col>
                        <a-col :md='8' :sm='24'>
                            <a-form-item >
                                <a-button type='primary' @click="getData">查询</a-button>
                                <a-button style="margin-left: 20px" type='' icon="download" @click="download">导出</a-button>
                            </a-form-item>
                        </a-col>
                    </a-row>
                </a-form>
            </div>  
            <a-table rowKey="id" :loading="isload" :columns="columns" :data-source="dataList" @change="handleTableChange" :pagination="{ total: total ,showSizeChanger: true,pageSize: size,}" size="small" :scroll="{ x: 1500, }">
                <div slot="isSign" slot-scope="isSign">
                    <div v-for="(item,i) in isSignList" :key="item.id">
                        <a-tag v-if="item.id == isSign" :color="colors[i%13]">{{item.name}}</a-tag>
                    </div>
                </div>
                <div slot="state" slot-scope="state">
                    <div v-for="(item,i) in stateList" :key="item.id">
                        <a-tag v-if="item.id == state" :color="colors[i%13]">{{item.name}}</a-tag>
                    </div>
                </div>
            </a-table>
        </a-card>

        <!-- 导出数据 -->
        <a-modal
            title="导出数据，每页10000条"
            :visible="downloadVisible"
            @ok="downladHandleOk"
            :confirm-loading="confirmLoading"
            @cancel="downloadVisible = false"
            >
            <div>
                <a-pagination v-model="current" :page-size.sync="pageSize" :total="total" show-less-items />
            </div>
        </a-modal>
    </div>
</template>

<script>
import Info from '../components/Info'
import { columns } from './table'
import { querySettlementDetailStatistics , downloadSettlementDetail } from '@/api/services/statistics';
export default {
    components: {
        Info,
    },
    data () {
        return {
            status: undefined,
            statusList: [
                { id: 0, name: '关闭'},
                { id: 1, name: '开启'},
            ],
            columns,
            page: 1,
            size: 10,
            total: 0,
            dataList: [],
            isload: false,
            projectName: '', // 项目名称
            customerName: '', // 客户名称
            companyName: '', // 公司名称
            settlementName: '', // 结算单名称
            query: '', // 用户信息
            totalAmount: 0, // 结算总金额
            successAmount: 0, // 成功金额
            failAmount: 0, // 失败金额
            successCount: 0, // 成功笔数
            failCount: 0, // 失败笔数
            totalCount: 0, // 结算笔数
            isSignList: [
                { id: 0, name: '未签约'},
                { id: 1, name: '已签约'}
            ],
            stateList: [
                { id: 0, name: '失败'},
                { id: 1, name: '成功'}
            ],
            colors: ['blue','purple','cyan','green','orange','red','pink','brown','indigo','lime','purple','teal','yellow','amber'],
            current: 1,
            downloadVisible: false,
            pageSize: 10000,
            confirmLoading: false,
        };
    },
    created(){
        this.getData();
    },
    methods: {
        // 日期筛选
        onDateChange(date,dateStr){
            console.log(date,dateStr)
            if(date.length > 0){
                this.startTime = `${dateStr[0]}`
                this.endTime = `${dateStr[1]}`
            }else{
                this.startTime = ''
                this.endTime = ''
            }
            this.getData()
        },
        // 获取统计列表数据
        getData(){
            let _this = this;
            _this.isload = true;
            const params = {
                'startTime': _this.startTime,
                'endTime': _this.endTime,
                'projectName': _this.projectName,
                'customerName': _this.customerName,
                'companyName': _this.companyName,
                'settlementName': _this.settlementName,
                'query': _this.query,
                'pageIndex': _this.page,
                'pageSize': _this.size,
            }
            querySettlementDetailStatistics(params)
            .then(res => {
                _this.isload = false;
                if(res.success){
                    _this.dataList = res.data.settlementDetails
                    _this.total = res.data.totalCount
                    // 数据统计
                    _this.totalAmount = res.data.statsInfo.totalAmount
                    _this.successAmount = res.data.statsInfo.successAmount
                    _this.failAmount = res.data.statsInfo.failAmount
                    _this.successCount = res.data.statsInfo.successCount
                    _this.failCount = res.data.statsInfo.failCount
                    _this.totalCount = res.data.statsInfo.totalCount
                }else{
                    _this.$notification['error']({
                        message: '温馨提示',
                        description: res.errMessage,
                        duration: 8
                    })
                }
            })
        },
        // 分页
        handleTableChange(pagination, filters, sorter) {
            console.log(pagination);
            this.page = pagination.current;
            this.size = pagination.pageSize;
            this.getData()
        },
        // 导出
        download(){
            this.downloadVisible = true;
            console.log('123123')
        },
        downladHandleOk(){
            let _this = this;
            _this.confirmLoading = true;
            const params = {
                'pageSize': _this.pageSize,
                'pageIndex': _this.current,
                'startTime': _this.startTime,
                'endTime': _this.endTime,
                'projectName': _this.projectName,
                'customerName': _this.customerName,
                'companyName': _this.companyName,
                'settlementName': _this.settlementName,
                'query': _this.query,
            }
            downloadSettlementDetail(params)
            .then(res => {
                _this.confirmLoading = false;
                if(res){
                    const content = res
                    console.log(res,"res")
                    let blob = new Blob([content], {type: 'application/x-excel'});
                    const fileName = `结算明细数据表.xlsx`
                    if ('download' in document.createElement('a')) { // 非IE下载
                        const elink = document.createElement('a')
                        elink.download = fileName
                        elink.style.display = 'none'
                        elink.href = URL.createObjectURL(blob)
                        document.body.appendChild(elink)
                        elink.click()
                        URL.revokeObjectURL(elink.href) // 释放URL 对象
                        document.body.removeChild(elink)
                    } else { // IE10+下载
                        navigator.msSaveBlob(blob, fileName)
                    }
                }else{
                    _this.$notification['error']({
                        message: '温馨提示',
                        description: res.errMessage,
                        duration: 8
                    })
                }
            })
        },
    }
}

</script>
<style scoped>

</style>